#if ( !defined __URL_SEED_HPP__ )
#define __URL_SEED_HPP__

#include <string>
#include <vector>

#include "sqlite3/sqlite3_wrapper.hpp"

#define SQL_CREATE_URLS_TABLE "CREATE TABLE t_urls(\
								id INTEGER PRIMARY KEY, \
								category INTEGER, \
								url VARCHAR(200) UNIQUE NOT NULL, \
								status INTEGER, \
								create_time DATE, \
								update_time DATE );"

#define SQL_UPDATE_URLS "UPDATE t_urls \
						SET \
							status=?, \
							update_time=DATETIME('NOW') \
						WHERE url=?;"

#define SQL_RESET_URLS "UPDATE t_urls \
						SET \
							status=0, \
							update_time=DATETIME('NOW')"

#define SQL_INSERT_URLS "INSERT INTO t_urls (\
							url, \
							category, \
							status, \
							create_time, \
							update_time \
						)VALUES( \
							?, \
							?, \
							0, \
							DATETIME('NOW'), \
							DATETIME('NOW') \
						);"

#define SQL_SELECT_URLS_WITH_STATUS "SELECT url,category,status,update_time FROM t_urls WHERE status=?;"
#define SQL_SELECT_URLS_WITH_URL "SELECT url,category,status,update_time FROM t_urls WHERE url=?;"
#define SQL_SELECT_URLS_WITH_CATEGORY "SELECT url,category,status,update_time FROM t_urls WHERE category=?;"
#define SQL_SELECT_URLS_COUNT_WITH_URL "SELECT count(*) FROM t_urls WHERE url=?;"


using namespace std;
using namespace sqlite3_wrapper;

class UrlSeed
{
public:
	UrlSeed ( const string& db_file, const string protocol, const string& host, const vector< string >& paths, const vector< string >& expressions );
	~UrlSeed ();

public:
	void update ( const string& url, const string& page_header, const string& page_content );
	void update ( const string& url, int status );
	void unread_urls ( vector < string >& urls );
	void split_url ( const string& url, string& protocol, string& host, string& dir, string& file );
protected:
	void create_database ();
	void search_content ( const string& url, const string& page_content, int category, const string& expr );
	void set_url ( const string& url, int category );

private:
	string _procotol;
	string _host;
	string _dir;
	vector < string > _expressions;
	string _db_filename;
	sqlite3_connection* _db_connection;

};

#endif // __URL_SEED_HPP__